Skip to content

Conversation

@mnwright
Copy link
Collaborator

@mnwright mnwright commented Feb 7, 2024

Adds a function to convert xgboost to rpf objects. Not very fast, it seems the bottleneck is get_leaf_bounds(), which could be implemented in C++.

Example:

library(randomPlantedForest)
library(xgboost)
library(ggplot2)
library(patchwork)

# Fit xgboost
x <- as.matrix(mtcars[, -1])
y <- mtcars$mpg
xg <- xgboost(data = x[1:26, ], label = y[1:26],
              params = list(max_depth = 4, eta = .1),
              nrounds = 10, verbose = 0)

# Convert
xgrpf <- convert_xgboost_rpf(xg, x, y)

# Prediction seems to be fairly similar
max(abs(predict(xg, x) - predict(xgrpf, x)))

library(glex)
xg_g <- glex(xg, x = x)
rpf_g <- glex(xgrpf, x = as.data.frame(x))

# Intercept is different
xg_g$intercept - rpf_g$intercept

# Components mostly differ in intercept
autoplot(xg_g, "wt") + autoplot(rpf_g, "wt")
autoplot(xg_g, "cyl") + autoplot(rpf_g, "cyl")

@jemus42
Copy link
Collaborator

jemus42 commented Feb 7, 2024

Wondering if glex should provide a purify S3 generic and a method for xgb, while this package can define the method for itself and just use it to call its internal method in C++.

Not that it matters much for the experimental part but I'd prefer to not have xgboost as an Imports here, maybe a Suggests but, well, might as well use glex for that functionality in any case.

@mnwright
Copy link
Collaborator Author

mnwright commented Feb 7, 2024

I agree. The C++ part (set_model) should stay here and the rest go to glex (or somewhere else). Just didn't want to have to two PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants